package quq.missq.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.util.LinkedList;

import quq.missq.Constants;
import quq.missq.R;
import quq.missq.activity.BigShootDetailActivity;
import quq.missq.activity.TopicDetailActivity;
import quq.missq.beans.BigShootBean;
import quq.missq.beans.SubscribleBean;
import quq.missq.utils.ImageLoadUtil;
import quq.missq.utils.StringUtils;
import quq.missq.utils.VolleyTool;
import quq.missq.views.NoScrollGridView;

/**
 * Created by River on 7/27/15.
 */
public class MySubscribleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_FOOTER = 0;
    private static final int TYPE_SUBSCRIBLE = 1;

    private Context mContext;
    private LinkedList<SubscribleBean.Result> results;
    private LayoutInflater mInflater;


    public MySubscribleAdapter(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(LinkedList<SubscribleBean.Result> results) {
        this.results = results;
    }

    public LinkedList<SubscribleBean.Result> getData() {
        return this.results;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        if (viewType == TYPE_FOOTER) {
            View view = mInflater.inflate(R.layout.foot_layout, null);
            return new FootViewHolder(view);
        } else if (viewType == TYPE_SUBSCRIBLE) {
            View view = mInflater.inflate(R.layout.my_subscrible_adapter, null);
            return new SubscribleHolder(view);
        } else {
            return null;
        }
    }


    ImageGridViewAdapter adapter;

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof SubscribleHolder) {
            SubscribleHolder holder = (SubscribleHolder) viewHolder;
            final SubscribleBean.Result result = results.get(position);
            int categoryId = result.getPost().getCategory().getId();
            int imgId = -1;
            int dazhaoOrTopic = -1; // 0 大招 1 话题
            if (categoryId == 1) {
                imgId = R.mipmap.icon_plan;
                dazhaoOrTopic = 0;
            } else if (categoryId == 2) {
                imgId = R.mipmap.icon_change;
                dazhaoOrTopic = 0;
            } else if (categoryId == 3) {
                imgId = R.mipmap.icon_night;
                dazhaoOrTopic = 0;
            } else if (categoryId == 5) {
                imgId = R.mipmap.icon_qa;
                dazhaoOrTopic = 1;
            } else {
                imgId = R.mipmap.icon_show;
                dazhaoOrTopic = 1;
            }
            if (dazhaoOrTopic == 0) {
                holder.leftType.setImageResource(R.mipmap.ts_dazhao);
                holder.dian.setVisibility(View.GONE);
                holder.contentImage.setVisibility(View.VISIBLE);
                holder.gridView.setVisibility(View.GONE);
                ImageLoadUtil.loadImage(holder.contentImage, Constants.IMAGE_HOST + result.getPost().getAttachs().get(0).getThumbnail(), R.mipmap.default_empty2);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.setClass(mContext, BigShootDetailActivity.class);
                        intent.putExtra("id", result.getPost().getId());
                        mContext.startActivity(intent);
                    }
                });
            } else {
                holder.contentImage.setVisibility(View.GONE);
                holder.gridView.setVisibility(View.VISIBLE);
                holder.leftType.setImageResource(R.mipmap.ts_huati);
                holder.dian.setVisibility(View.VISIBLE);
                adapter = new ImageGridViewAdapter(mContext, result.getPost().getAttachs());
                holder.gridView.setAdapter(adapter);
                holder.gridView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            Intent intent = new Intent();
                            intent.setClass(mContext, TopicDetailActivity.class);
                            intent.putExtra("id", result.getPost().getId());
                            mContext.startActivity(intent);
                            return false;
                        }
                        return false;
                    }
                });
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.setClass(mContext, TopicDetailActivity.class);
                        intent.putExtra("id", result.getPost().getId());
                        mContext.startActivity(intent);
                    }
                });
            }
            int gender = result.getPost().getAuthor().getGender();
            if (gender == 1) {
                holder.gender.setImageResource(R.mipmap.boy_icon);
            } else {
                holder.gender.setImageResource(R.mipmap.girl_icon);
            }
            int i = result.getPost().getAuthor().getAvatar().indexOf('?');
            String url = result.getPost().getAuthor().getAvatar().substring(0, i);
            ImageLoadUtil.loadImage(holder.avtar, Constants.IMAGE_HOST + url, R.mipmap.empty_photo);
            //holder.avtar.setImageUrl(Constants.IMAGE_HOST + url, VolleyTool.getImageLoader());
            holder.name.setText(result.getPost().getAuthor().getNkname());
            holder.level.setText("level " + result.getPost().getAuthor().getLevel());
            switch (result.getPost().getAuthor().getAuth()) {
                case 0:
                    holder.userType.setText("普通用户");
                    break;
                case 1:
                    holder.userType.setText("加 V");
                    break;
                case 2:
                    holder.userType.setText("女 神");
                    break;
            }

            holder.topicType.setImageResource(imgId);
            String friendly_time = StringUtils.friendly_time(result.getCreated());
            holder.time.setText(friendly_time);
            holder.content.setText(result.getPost().getTitle());
        } else if (viewHolder instanceof FootViewHolder) {
            FootViewHolder footHolder = (FootViewHolder) viewHolder;
            footHolder.itemView.setTag("footView");
            footHolder.itemView.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemViewType(int position) {
        //return super.getItemViewType(position);
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_SUBSCRIBLE;
            // return results.get(position).getType();
        }
    }

    @Override
    public int getItemCount() {
        return results.size() + 1;
    }

    class SubscribleHolder extends RecyclerView.ViewHolder {

        ImageView avtar;
        TextView name;
        TextView userType;
        TextView level;
        ImageView topicType;
        TextView time;
        TextView content;
        ImageView gender;
        NoScrollGridView gridView;
        ImageView leftType;
        ImageView dian;
        ImageView contentImage;

        public SubscribleHolder(View itemView) {
            super(itemView);
            avtar = (ImageView) itemView.findViewById(R.id.avtar);
            name = (TextView) itemView.findViewById(R.id.name);
            userType = (TextView) itemView.findViewById(R.id.userType);
            level = (TextView) itemView.findViewById(R.id.level);
            topicType = (ImageView) itemView.findViewById(R.id.topicType);
            time = (TextView) itemView.findViewById(R.id.time);
            content = (TextView) itemView.findViewById(R.id.content);
            leftType = (ImageView) itemView.findViewById(R.id.leftType);
            dian = (ImageView) itemView.findViewById(R.id.dian);
            gender = (ImageView) itemView.findViewById(R.id.gender);
            gridView = (NoScrollGridView) itemView.findViewById(R.id.gridView);
            contentImage = (ImageView) itemView.findViewById(R.id.contentImage);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder {

        public FootViewHolder(View itemView) {
            super(itemView);
        }

    }

}
